import { Config } from "../../Config"
import { RoomState } from "../../hall/Room"
import Logger from "../../utils/Logger"
import { Player } from "../core/Player"
import { Point, Terrain } from "../core/Point"
import GameUtils from "../GameUtils"
import Utils from "../GameUtils"
import { BaseHandler } from "./BaseHandler"

export class 野猪Handler extends BaseHandler{

    /**
     * 野猪冲撞，地形错误
     */
    async impact(data:any,player:Player) {
        this.handlerMgr.spendAction(player,Config.COST_野猪_IMPACT)
        let startPoint = this.gameMgr.map_point.get(data.start)
        let endPoint = this.gameMgr.map_point.get(data.end)
        let big_start = startPoint.big
        big_start.actionOnce()
        this.gameMgr.sendBroadcast(GameUtils.getObj("play_sound",{"sound":"野猪_冲锋","username":player.username}))
        let list = this.getPointList(data.start, data.end)
        //1.地形不对，停下来
        //2.有大子，直接杀过去
        //3.移动一下，保证野猪能获取视野
        for(let point of list) {
            endPoint = point
            if ([Terrain.SHAN,Terrain.JING,Terrain.SHUI].includes(point.terrain)) {
                return
            }
            if (point.small != null) {
                point.small.die(player)
                //有可能把自己父系干掉了，自己也没了，直接返回
                if (big_start.is_death) {
                    return
                }
            }else if (point.big != null) {
                let big_end = endPoint.big
                if (big_end.attacked(player)) {
                    //有可能把自己父系干掉了，自己也没了，直接返回
                    if (big_start.is_death) {
                        return
                    }
                } else {
                    big_start.die(big_end.player, endPoint.id)
                    return
                }
            }
            startPoint.big = null
            endPoint.big = big_start
            big_start.point = endPoint
            this.gameMgr.sendBroadcast(Utils.getObj("big_move",{"start":startPoint.id,"end":endPoint.id}))
            startPoint = endPoint
            big_start.update()
            await this.delay(300)
        }
        if (startPoint == endPoint) {
            Logger.log("走到头了")
            return
        }
    }

    /**
     * 一共六个方向，一直获取point,直到获取不到
     */
    getPointList(start:String, end:String) {
        let startPoint = this.gameMgr.map_point.get(start)
        let endPoint = this.gameMgr.map_point.get(end)
        let direction_x = endPoint.position.x - startPoint.position.x
        let direction_y = endPoint.position.y - startPoint.position.y
        let list = Array<Point>()
        let point = endPoint
        while(point != null) {
            list.push(point)
            let x = point.position.x + direction_x
            let y = point.position.y + direction_y
            point = this.gameMgr.map_point.get(Utils.getPointId(x,y))
        }
        return list
    }

    // 延迟一下
    delay(ms: number): Promise<void> {
        return new Promise(resolve => {
            if (this.gameMgr?.room == null || this.gameMgr.room.state == RoomState.CLOSE) return
            setTimeout(resolve, ms);
        });
    }

}